<template>
    <lay-container :fluid="true" style="padding: 10px;padding-top: 10px;">
        <lay-card style="padding:10px;">
            <lay-row style="margin-bottom:10px;">
                <lay-breadcrumb>
                    <lay-breadcrumb-item title="发文登记"></lay-breadcrumb-item>
                    <lay-breadcrumb-item :title="addoredittip"></lay-breadcrumb-item>
                </lay-breadcrumb>
            </lay-row>
            <lay-row space="10">
                <lay-form :model="form">
                    <lay-col :md="24">
                        <lay-form-item label="" prop="cid">
                            <lay-col md="6">
                                <lay-quote>{{ catInfo.name }} <h2 style="color:#FF5722;font-family: Impact;">
                                        第 {{ docNumber }} 号</h2>
                                    <span v-if="id == 0" style="color:#c2c2c2;"> (此文号只是系统推测，请以提交后为准)</span>
                                </lay-quote>
                            </lay-col>
                        </lay-form-item>
                        <lay-col md="18">
                            <lay-form-item label="公文标题" prop="title" required required-error-message="公文标题不能空">
                                <lay-input v-model="form.title"></lay-input>
                            </lay-form-item>
                        </lay-col>
                        <lay-col md="18">
                            <lay-col md="6">
                                <lay-form-item label="密级" prop="status">
                                    <lay-select v-model="form.secretLevel" style="width:130px;">
                                        <lay-select-option v-for="status in secretLevelDicts" :value="status.key"
                                            :label="status.value">
                                        </lay-select-option>
                                    </lay-select>
                                </lay-form-item>
                            </lay-col>
                            <lay-col md="9">
                                <lay-form-item label="报送/发往部门" prop="department">
                                    <lay-input v-model="form.department"></lay-input>
                                </lay-form-item>
                            </lay-col>
                            <lay-col md="9">
                                <lay-form-item label="报送领导" prop="leader">
                                    <lay-input v-model="form.leader"></lay-input>
                                </lay-form-item>
                            </lay-col>
                        </lay-col>
                        <lay-col md="18">
                            <lay-col md="6">
                                <lay-form-item label="拟稿" prop="auther">
                                    <lay-input v-model="form.auther"></lay-input>
                                </lay-form-item>
                            </lay-col>
                            <lay-col md="6">
                                <lay-form-item label="校对" prop="proofread">
                                    <lay-input v-model="form.proofread"></lay-input>
                                </lay-form-item>
                            </lay-col>
                            <lay-col md="6">
                                <lay-form-item label="初审" prop="earlierExa">
                                    <lay-input v-model="form.earlierExa"></lay-input>
                                </lay-form-item>
                            </lay-col>
                            <lay-col md="6">
                                <lay-form-item label="签发" prop="examine">
                                    <lay-input v-model="form.examine"></lay-input>
                                </lay-form-item>
                            </lay-col>
                        </lay-col>
                        <lay-form-item label="内容" prop="content">
                            <lay-col md="24">
                                <WangEditor v-model="form.content"></WangEditor>
                            </lay-col>
                        </lay-form-item>
                        <div v-permission="PerKey.Upload_Upload">
                            <lay-field title="附件">
                                <UploadAttments v-model="form.attments" :attList="getAttments" />
                            </lay-field>
                        </div>
                        <lay-col md="18">
                            <lay-form-item label="备注" prop="remarks">
                                <lay-input v-model="form.remarks"></lay-input>
                            </lay-form-item>
                        </lay-col>
                        <lay-form-item v-if="btnHide">
                            <lay-button @click="submitClick" type="primary" v-permission="PerKey.DocPost_AddOrEdit"
                                prefix-icon="layui-icon-ok">
                                {{ addoredittip }}
                            </lay-button>
                        </lay-form-item>
                    </lay-col>
                </lay-form>
            </lay-row>
        </lay-card>
    </lay-container>
</template>

<script  lang="ts" setup>
import { reactive, ref, toRefs } from "vue";
import { layer } from "@layui/layer-vue";
import { getEdit, putEdit, postAdd } from "../../../../api/module/oas/docpost";
import { PerKey } from "../../../../library/permissionConst"
import { useRoute, useRouter } from "vue-router";
import WangEditor from '../../../../layouts/Editor/wangEditor.vue'
import UploadAttments from '../../../../layouts/Upload/UploadAttments.vue'
import { DocPost, DocCat } from '../../../../types/doc'
import { Attment } from "../../../../types/attment"
import { Result } from "../../../../types/result"
import Util from '../../../../library/Util'

const router = useRouter();
const data = reactive({
    form: <DocPost><unknown>[],
    getAttments: [<Attment>{}],
});
const { form, getAttments } = toRefs(data);
const route = useRoute();
const id = ref(Number(route.query.Id))
const cid = ref(Number(route.query.Cid))
let addoredittip = ref({});

const catInfo = ref(<DocCat><unknown>[]);
const docNumber = ref();
const secretLevelDicts = ref()
secretLevelDicts.value = Util.dictfilter("Sys_SecretLevel")

const get =  async () => {
    await getEdit(id.value, cid.value).then((res) => {
        catInfo.value = res.data.docCat;
        if (id.value == 0) {
            let lastDoc = <DocPost><unknown>[];
            if (res.data.lastDoc) {
                lastDoc = res.data.lastDoc
            }
            addoredittip.value = "新增公文";
            docNumber.value = res.data.newNumber
            form.value = {
                id: 0,
                cid: cid.value,
                title: "",
                attments: "",
                secretLevel: 1,
                department: lastDoc.department ?? "",
                leader: lastDoc.leader ?? "",
                examine: lastDoc.examine ?? "",
                auther: lastDoc.auther ?? "",
                proofread: lastDoc.proofread ?? "",
                earlierExa: lastDoc.earlierExa ?? "",
                remarks: "",
                content: ""
            }
            getAttments.value = []
            return;
        }
        else {
            addoredittip.value = "编辑公文";
            if (res.status == 1) {
                form.value = res.data.data
                docNumber.value = res.data.data.docNumber
                getAttments.value = res.data.attments ?? []
            }
        }
    });
}

get()

const btnHide = ref(true)

const submitClick = function () {
    if (!form.value.title || form.value.secretLevel < 1) {
        layer.msg("公文名称或者必要元素不能空");
        return;
    }
    //if (form.value.attments != null)//对附件进行类型转换
       // form.value.attments = JSON.stringify(form.value.attments)
    if (form.value.id < 1) {
        postAdd(form.value).then((res) => {
            done(res);
        });
    } else {
        putEdit(form.value).then((res) => {
            done(res);
        });
    }
};
const done = (res: Result) => {
    if (res.status == 1) {
        btnHide.value = false //隐藏按钮
        layer.msg(res.msg, { time: 1000, icon: 1 }, () => {
            router.push({ path: "/oas/doc/post/list", query: { Cid: form.value.cid } });
        });
    }
    else {
        layer.msg(res.msg, { icon: 2 });
        return false;
    }
};
</script>
<style scoped>
.tip {
    color: gray;
    line-height: 38px;
    height: inherit;
    padding-left: 10px;
}
</style>